package com.javasm.dao.impl;

import com.javasm.dao.GameDao;
import com.javasm.entity.AdminUser;
import com.javasm.entity.Game;
import com.javasm.entity.sql.GameManageSql;
import com.javasm.entity.sql.LoginManageSql;
import com.javasm.entity.sql.MenuManageSql;
import com.javasm.util.DruidUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/20 11:01
 * @since JDK 1.8
 */
public class GameDaoImpl implements GameDao {
    /**
     * 分页查询游戏列表
     *
     * @param page     当前页码
     * @param pageSize 每页查询条数
     * @return
     */
    @Override
    public List<Game> selectGameByPage(int page, int pageSize) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        List<Game> gameList = null;
        try {
            gameList = queryRunner.query(GameManageSql.SELECT_GAME_BY_PAGE, new BeanListHandler<>(Game.class), (page - 1) * pageSize, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gameList;
    }

    /**
     * 查询游戏信息总记录数
     *
     * @return
     */
    @Override
    public int selectTotalCount() {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int totalCount = 0;
        try {
            totalCount = ((Long) queryRunner.query(GameManageSql.SELECT_GAME_TOTALCOUNT, new ScalarHandler<>())).intValue();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return totalCount;
    }

    /**
     * 新增游戏信息
     *
     * @param game 要新增的游戏对象
     * @return
     */
    @Override
    public int insertGameInfo(Game game) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int result = 0;
        try {
            result = queryRunner.update(GameManageSql.INSERT_GAME, game.getName(), game.getTitle(),
                    game.getSize(), game.getGameicon(), game.getDetailpic(), game.getIosurl(),
                    game.getAndroidurl(), game.getTypename(), game.getType(), game.getStatus(),
                    game.getPlatform(), game.getDesc());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过id修改菜单信息
     *
     * @param game 要修改的菜单对象
     * @return
     */
    @Override
    public int updateGameInfo(Game game) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int result = 0;
        try {
            result = queryRunner.update(GameManageSql.UPDATE_GAME_BY_GID, game.getName(), game.getTitle(),
                    game.getSize(), game.getGameicon(), game.getDetailpic(), game.getIosurl(),
                    game.getAndroidurl(), game.getTypename(), game.getType(), game.getStatus(),
                    game.getPlatform(), game.getDesc(),game.getGid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过id删除菜单
     *
     * @param gid 要删除的用户id
     * @return
     */
    @Override
    public int deleteGameInfo(String gid) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        int result = 0;
        try {
            result = queryRunner.update(GameManageSql.DELETE_GAME_BY_GID,gid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过id查询游戏信息
     * @param gid 游戏id
     * @return
     */
    @Override
    public Game getGameById(String gid) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        Game game = new Game();
        try {
            game = queryRunner.query(GameManageSql.SELECT_GAME_BY_GID,new BeanHandler<>(Game.class),gid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return game;
    }

}
